package com.itzdm.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.entity.Category;
import com.itzdm.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询分类
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询分类")
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize) {
        log.info("分页查询分类：{}{}", page, pageSize);
        Page p = categoryService.pageCate(page,pageSize);
        return Result.success(p);
    }

    /**
     * 新增分类接口
     * @param category
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类接口")
    public Result<String> save(@RequestBody Category category){
        log.info("新增分类：{}", category.toString());
        categoryService.save(category);
        return Result.success("添加成功");
    }


    /**
     * 删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(Long ids){
        log.info("删除分类：{}", ids.toString());
        categoryService.removeCateById(ids);
        return Result.success("删除成功");
    }


    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类接口")
    public Result<String> change(@RequestBody Category category){
        log.info("修改分类信息{}", category.toString());
        categoryService.updateById(category);
        return Result.success("修改成功");
    }


    /**
     * 根据条件查询分类接口
     * @param category
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询分类接口")
    public Result<List<Category>> list(Category category){
        log.info("根据条件查询分类：{}",category.toString());
        List<Category> list = categoryService.listCates(category);
        return Result.success(list);
    }
}
